/**
 * Created by oneminute on 7/26/14.
 */

var assert = require("assert");
var should = require("should");
var async = require("async");
var logger = require("../../lib/utils/logger");
var testutils = require("../testutils");
var lz = require("../../lib/utils/modules")();

describe("测试Message模块。", function () {
    var messages = [];

    before(function(done) {
        var tasks = [];
        var senderid = lz.Utils.getUUID("u_");
        var recieverid = lz.Utils.getUUID("u_");
        for (var i = 0; i < 10; i++) {
            tasks.push(function(_cb) {
                var msg = new lz.Message({
                    header: {
                        id: "m_00" + i,
                        senderid: senderid,
                        recieverid: recieverid
                    },
                    body: {
                        content: "test" + i
                    }
                });
                msg.save(function() {
                    messages.push(msg);
                    _cb(null);
                });
            });
        }
        async.series(tasks, function() {
            done();
        });
    });

    after(function(done) {
        var tasks = [];
        messages.forEach(function(_msg) {
            tasks.push(function(_cb) {
                _msg.delete(function() {
                    _cb(null);
                });
            });
        });
        async.series(tasks, function() {
            done();
        });
    });

    it("获取所有的离线消息。", function (done) {
        var msg = new lz.Message({header: { recieverid: messages[0].header.recieverid}});
        msg.loadOfflineMessages(function(_err, _messages) {
            testutils.check(done, function() {
                assert.equal(_messages.length, messages.length);
            });
        });
    });
});
